let express = require("express")
let router = express.Router();
let cinemaService = require("../service/cinemaService.js")

router.get("/cinema",async function(req,res,next){
     let { key, val, page, limit } = req.query;
     let filter = null;
     if (key != null) {
       if (key == "cinemasName") {
         filter = {cinemasName : {$regex: new RegExp(val) } };
       }
       if (key == "cinemasAddr") {
         filter = { cinemasAddr: val };
       }
     }
    let resul = await cinemaService.getCinema(filter, page, limit);
    res.send(resul);
});
//新增
router.post("/addCinema",async function(req,res){
  let {cinemasName,
       cinemasAddr,
       cinemasTel,
       cinemasUrl,
       screenName,
       sereenSeat}=req.body
      let screen={screenName,sereenSeat}
 let resul = await cinemaService.addCinema({cinemasName,cinemasAddr,cinemasTel,cinemasUrl,screen})
 res.send(resul)
})
//删除
router.delete("/delCinema/:id",async function(req,res){
     let id = req.params.id
     let resul = await cinemaService.deleteById(id)
     res.send(resul);
 
})
//修改
router.put("/update/:id",async function(req,res){
  let id = req.params.id;
  let {cinemasName,
    cinemasAddr,
    cinemasTel,
    cinemasUrl,
     screen}=req.body
     let result = await cinemaService.updateCinema(id,{cinemasName,cinemasAddr,cinemasTel,cinemasUrl,screen} )
     console.log(cinemasName,cinemasAddr,cinemasTel,cinemasUrl,screen);
     res.send(result)

})

router.get("/sc/:id",async function(req,res){
  let id = req.params.id
  console.log(id);
 let data = await cinemaService.screen({_id:id})
 res.send(data)
 console.log(data);
})
module.exports=router;